package com.thread;

import java.util.concurrent.TimeUnit;

public class ThreadException004 {
    int count = 0
            ;// 这里在他t1异常，默认释放锁之后，count 是全局变量，在t2进入时候，count=6 对业务就会造成影响

    synchronized void m(){
        System.out.println(Thread.currentThread().getName()+" start");

        while(true){
            ++count;
            System.out.println(Thread.currentThread().getName() + " count="+count);

            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            if(count == 5){
                int i = 1/0;//exception  默认释放锁
                System.out.println("i="+i);
            }
        }
    }

    public static void main(String[] args) {
        ThreadException004 t = new ThreadException004();

        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                t.m();
            }
        };

        new Thread(runnable,"t1").start();;

        new Thread(runnable,"t2").start();;

    }
}
